import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordFrequencyStatistics {

    public static void main(String[] args) {
        try {
            String filePath = "your_article.txt"; // 替换为实际路径
            Map.Entry<String, Integer> result = findMostFrequentWord(filePath);
            System.out.printf("最高频单词: %s (出现次数: %d)%n", result.getKey(), result.getValue());
        } catch (IOException e) {
            System.out.println("文件读取失败: " + e.getMessage());
        }
    }

    private static Map.Entry<String, Integer> findMostFrequentWord(String filePath) throws IOException {
        // 读取文件内容并转为小写
        String content = new String(Files.readAllBytes(Paths.get(filePath))).toLowerCase();

        // 使用正则表达式匹配单词
        Pattern pattern = Pattern.compile("\\b[a-z]+\\b");
        Matcher matcher = pattern.matcher(content);

        // 统计词频
        Map<String, Integer> wordCount = new HashMap<>();
        while (matcher.find()) {
            String word = matcher.group();
            wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
        }

        // 找出最高频单词
        return Collections.max(
                wordCount.entrySet(),
                Map.Entry.comparingByValue()
        );
    }
}
